package com.oa.notice.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.oa.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.oa.common.annotation.Log;
import com.oa.common.core.controller.BaseController;
import com.oa.common.core.domain.AjaxResult;
import com.oa.common.enums.BusinessType;
import com.oa.notice.domain.OaNotice;
import com.oa.notice.service.IOaNoticeService;
import com.oa.common.utils.poi.ExcelUtil;
import com.oa.common.core.page.TableDataInfo;

/**
 * 通知公告Controller
 * 
 * @author li.ziye
 * @date 2022-05-11
 */
@RestController
@RequestMapping("/notice/notice")
public class OaNoticeController extends BaseController
{
    @Autowired
    private IOaNoticeService oaNoticeService;

    /**
     * 查询通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaNotice oaNotice)
    {
        startPage();
        List<OaNotice> list = oaNoticeService.selectOaNoticeList(oaNotice);
        return getDataTable(list);
    }

    /**
     * 查询我的通知公告列表
     * 发送人或接收人均为当前登录账号id的全部通知
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:list')")
    @GetMapping("/myList")
    public TableDataInfo myList(OaNotice oaNotice)
    {
        startPage();
        oaNotice.setReceiverId(SecurityUtils.getUserId().toString());
        oaNotice.setSenderId(SecurityUtils.getUserId());
        List<OaNotice> list = oaNoticeService.selectMyOaNoticeList(oaNotice);
        return getDataTable(list);
    }

    /**
     * 导出通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:export')")
    @Log(title = "通知公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaNotice oaNotice)
    {
        List<OaNotice> list = oaNoticeService.selectOaNoticeList(oaNotice);
        ExcelUtil<OaNotice> util = new ExcelUtil<OaNotice>(OaNotice.class);
        util.exportExcel(response, list, "通知公告数据");
    }

    /**
     * 获取通知公告详细信息
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Long noticeId)
    {
        OaNotice oaNotice = oaNoticeService.selectOaNoticeByNoticeId(noticeId);
        if("0".equals(oaNotice.getReadStatus())){
            System.out.println("更改阅读状态");
            oaNotice.setReadStatus("1");
            oaNoticeService.updateOaNotice(oaNotice);
        }
        return AjaxResult.success(oaNotice);
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaNotice oaNotice)
    {
        return toAjax(oaNoticeService.insertOaNotice(oaNotice));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaNotice oaNotice)
    {
        return toAjax(oaNoticeService.updateOaNotice(oaNotice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('notice:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(oaNoticeService.deleteOaNoticeByNoticeIds(noticeIds));
    }
}
